/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.page.category;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableSetContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.Section;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.common.CommonUtils;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import cuongnh.project.japanesediary.vocabulary.model.Kanji;

/**
 * @author dejan
 * 
 *         To change the template for this generated type comment go to Window -
 *         Preferences - Java - Code Generation - Code and Comments
 */
public class KanjiCategoryDetailsPage implements IDetailsPage {
	private DataBindingContext m_bindingContext;
	private IManagedForm mform;
	private Section sctnNewSection;
	private Category input;
	private Table table;
	private TableViewer kanjiList;
	private CommonServices commonServices;
	private TableColumn tblclmnKanji;
	private TableViewerColumn tableViewerColumn;
	private TableColumn tblclmnVietnameseMeaning;
	private TableViewerColumn tableViewerColumn_1;
	private TableColumn tblclmnVietnameseExplaination;
	private TableViewerColumn tableViewerColumn_2;

	public KanjiCategoryDetailsPage(
			CommonServices commonServices) {
		this.commonServices = commonServices;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.IDetailsPage#initialize(org.eclipse.ui.forms.
	 * IManagedForm)
	 */
	public void initialize(IManagedForm mform) {
		this.mform = mform;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.forms.IDetailsPage#createContents(org.eclipse.swt.widgets
	 * .Composite)
	 */
	public void createContents(Composite parent) {
		parent.setLayout(new GridLayout(1, false));

		sctnNewSection = mform.getToolkit().createSection(parent,
				Section.EXPANDED | Section.TITLE_BAR);
		sctnNewSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		mform.getToolkit().paintBordersFor(sctnNewSection);
		sctnNewSection.setText("Category:");

		Composite composite = new Composite(sctnNewSection, SWT.NONE);
		mform.getToolkit().adapt(composite);
		mform.getToolkit().paintBordersFor(composite);
		sctnNewSection.setClient(composite);
		composite.setLayout(new GridLayout(1, false));

		kanjiList = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table = kanjiList.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		tableViewerColumn = new TableViewerColumn(kanjiList, SWT.NONE);
		tblclmnKanji = tableViewerColumn.getColumn();
		tblclmnKanji.setWidth(83);
		tblclmnKanji.setText("Kanji");
		
		tableViewerColumn_1 = new TableViewerColumn(kanjiList, SWT.NONE);
		tblclmnVietnameseMeaning = tableViewerColumn_1.getColumn();
		tblclmnVietnameseMeaning.setWidth(235);
		tblclmnVietnameseMeaning.setText("Vietnamese meaning");
		
		tableViewerColumn_2 = new TableViewerColumn(kanjiList, SWT.NONE);
		tblclmnVietnameseExplaination = tableViewerColumn_2.getColumn();
		tblclmnVietnameseExplaination.setWidth(244);
		tblclmnVietnameseExplaination.setText("Vietnamese explaination");

		CommonUtils.kanjisHookDoubleClickCommand(kanjiList);

	}


	private void update() {
		// m_bindingContext = initDataBindings();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.forms.IDetailsPage#inputChanged(org.eclipse.jface.viewers
	 * .IStructuredSelection)
	 */
	public void selectionChanged(IFormPart part, ISelection selection) {
		IStructuredSelection ssel = (IStructuredSelection) selection;
		if (ssel.size() == 1) {
			input = (Category) ssel.getFirstElement();
			// Get the category with updated vocabularies list
			input = commonServices.getCategoryDao()
					.getCategoryWithKanjis(input.getCategoryId());
			sctnNewSection.setText("Category: " + input.getCategoryName());
			m_bindingContext = initDataBindings();
		} else
			input = null;

		update();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.IDetailsPage#commit()
	 */
	public void commit(boolean onSave) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.IDetailsPage#setFocus()
	 */
	public void setFocus() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.IDetailsPage#dispose()
	 */
	public void dispose() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.IDetailsPage#isDirty()
	 */
	public boolean isDirty() {
		return false;
	}

	public boolean isStale() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.IDetailsPage#refresh()
	 */
	public void refresh() {
		update();
	}

	public boolean setFormInput(Object input) {
		return false;
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableSetContentProvider setContentProvider = new ObservableSetContentProvider();
		IObservableMap[] observeMaps = PojoObservables.observeMaps(
				setContentProvider.getKnownElements(), Kanji.class,
				new String[] { "kanji", "vnMeaning", "vnExplaination" });
		kanjiList.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		kanjiList.setContentProvider(setContentProvider);
		//
		IObservableSet vocabulariesInputObserveSet = BeanProperties.set(
				"kanjis").observe(input);
		kanjiList.setInput(vocabulariesInputObserveSet);
		//
		return bindingContext;
	}
}
